package com.yonyou.iuap.base.impl;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import com.yonyou.iuap.base.dao.UserDao;
import com.yonyou.iuap.base.intf.IUserAdminService;
import com.yonyou.iuap.base.utils.BaseToolsUtils;
import com.yonyou.iuap.base.vo.UserVO;
import com.yonyou.iuap.shell.dao.BdCategoryRelationEntityDao;
import com.yonyou.iuap.shell.pub.RequestDto;
import com.yonyou.iuap.shell.pub.ResultDto;
import com.yonyou.iuap.utils.PropertyUtil;
@Service(value="Service-User-Admin")
public class UserAdminServiceImpl implements IUserAdminService {
	private final String powerFileName="/properties/power.properties";
	@Autowired
	@Qualifier("Dao-User-Admin")
	private UserDao userdao;
	@Autowired 
	@Qualifier("Dao-BdCategoryRelationEntityDao")
	private BdCategoryRelationEntityDao bdCategoryRelationEntityDao;
	public UserVO selectByCondition(String wheresql) {
		List<UserVO> listvo = userdao.selectByCondition(wheresql);
		return listvo==null?null:listvo.get(0);
	}
	public ResultDto selectByConditionToList(String whereSql) {
		ResultDto rsDto = new ResultDto();
		
		List<UserVO> listvo = userdao.selectByCondition(whereSql);
		rsDto.setStatus(0);
		rsDto.setMessage("查询成功!");
		rsDto.setData(listvo);
		rsDto.setCount(listvo==null?0:listvo.size());
		rsDto.setNum(listvo==null?0:listvo.size());
		return rsDto;
	}
	public ResultDto selectByConditionPage(RequestDto request) {
		ResultDto rsDto = new ResultDto();
		
		List<UserVO> listvo = userdao.selectUserByPage(request);
		//查询合计数
		if(StringUtils.isEmpty(request.getParams())){
			request.setParams(" 1=1 ");
		}
		List<UserVO> countList = userdao.selectByCondition(request.getParams());
		rsDto.setStatus(0);
		rsDto.setMessage("查询成功!");
		rsDto.setData(listvo);
		rsDto.setCount(countList==null?0:countList.size());
		rsDto.setNum(listvo==null?0:listvo.size());
		return rsDto;
	}
	//系统及类别管理员
	public ResultDto checkUserIsSysManager(String pk_user) {
		ResultDto rsDto = new ResultDto();
		try{
			//根据userid查询用户的编码
			List<UserVO> vos = userdao.selectByCondition(" pk_user='"+pk_user+"'");
			String usercode="";
			if(vos!=null && vos.size()>0){
				usercode = vos.get(0).getUser_code();
				//判断用户是否有权限进行基础档案的维护
//				String userList = BaseToolsUtils.getPropertiesValue(powerFileName, "userList");
				String userList = BaseToolsUtils.getPropertiesValue("encrypt_userList");
				if(!StringUtils.isEmpty(userList)){
					if(userList.contains(usercode)){
						rsDto.setStatus(0);
						rsDto.setMessage("权限校验通过!");
						rsDto.setData(usercode);
					}else{
						String whereSql = " 1=1";
						whereSql += " and (pk_user = '"+pk_user+"')";
						List<UserVO> listvo = bdCategoryRelationEntityDao.selectByCondition(whereSql);
						if(listvo==null&&listvo.size()==0){
							rsDto.setStatus(-2);
							rsDto.setMessage("该用户无权进行此操作!");
							rsDto.setData(usercode);
						}else{
							rsDto.setStatus(0);
							rsDto.setMessage("权限校验通过!");
							rsDto.setData(usercode);
						}
					}
				}
			}else{
				//如果用户么有找到
				rsDto.setStatus(-2);
				rsDto.setMessage("该用户无权进行此操作!");
				rsDto.setData(usercode);
			}
			
		}catch(Exception e){
			rsDto.setStatus(-2);
			rsDto.setMessage("操作异常："+e.getMessage());
		}
		return rsDto;
	}
	
	public ResultDto checkUserIsSys(String pk_user){
		ResultDto rsDto = new ResultDto();
		try{
			//根据userid查询用户的编码
			List<UserVO> vos = userdao.selectByCondition(" pk_user='"+pk_user+"'");
			String usercode="";
			if(vos!=null && vos.size()>0){
				usercode = vos.get(0).getUser_code();
				//判断用户是否有权限进行基础档案的维护
//				String userList = BaseToolsUtils.getPropertiesValue(powerFileName, "userList");
				String userList = BaseToolsUtils.getPropertiesValue("encrypt_userList");
				if(!StringUtils.isEmpty(userList)){
					if(userList.contains(usercode)){
						rsDto.setStatus(0);
						rsDto.setMessage("权限校验通过!");
						rsDto.setData(usercode);
					}else{
						rsDto.setStatus(-2);
						rsDto.setMessage("该用户无权进行此操作!");
						rsDto.setData(usercode);
					}
				}
			}else{
				//如果用户么有找到
				rsDto.setStatus(-2);
				rsDto.setMessage("该用户无权进行此操作!");
				rsDto.setData(usercode);
			}
			
		}catch(Exception e){
			rsDto.setStatus(-2);
			rsDto.setMessage("操作异常："+e.getMessage());
		}
		return rsDto;
	}
	
}
